R/Boundary map file.R

---
  title: "Boundary Change 2019"
output:
  flexdashboard::flex_dashboard:
  orientation: columns
vertical_layout: fill

runtime: shiny
---


# install necessary packages
#install.packages( c( "shiny", "leaflet", "mapview" ) )

# load necessary packages
library( shiny )
library( leaflet )
library( mapview )
library(rsconnect)
library(htmlwidgets)
library(rgdal)

#webshot::install_phantomjs()
if (is.null(suppressMessages(webshot:::find_phantom()))) { webshot::install_phantomjs() }
#library(webshot)

area1 <- readOGR("Merthyr Tydfil LA 2011.TAB")
area1 <- spTransform(area1, CRS("+proj=longlat +init=epsg:27700"))

area2 <-readOGR("Rhondda Cynon Taff LA 2011.TAB")
area2 <- spTransform(area2, CRS("+proj=longlat +init=epsg:27700"))

area3 <-readOGR("Bridgend LA 2011.TAB")
area3 <- spTransform(area3, CRS("+proj=longlat +init=epsg:27700"))

ui <- fluidPage(
  leafletOutput( outputId = "map", height=700),
  downloadButton( outputId = "dl")
)

server <- function(input, output, session) {

  # Create foundational leaflet map
  # and store it as a reactive expression
  foundational.map <- reactive({

    leaflet() %>% # create a leaflet map widget

      addTiles( urlTemplate = "http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png",attribution='© OpenStreetMap contributors, CC-BY-SA. Contains OS data © Crown Copyright and database right 2018. Ordnance Survey 100044810. Contains Royal Mail data © Royal Mail copyright and Database right 2018. Produced by Public Health Wales Observatory' )  %>%

      setView( lng=-3.493790
               ,lat =51.650660
               ,zoom =9) %>%

      addPolygons(data=area1,weight=2,col = 'blue',
                  highlightOptions = highlightOptions(color='blue', opacity = 1, weight = 2, fillOpacity = 0, bringToFront = TRUE, sendToBack = TRUE),
                  label ="Merthyr Tydfil") %>%
      addPolygons(data=area2,weight=2,col = 'blue',
                  highlightOptions = highlightOptions(color='blue', opacity = 1, weight = 2, fillOpacity = 0, bringToFront = TRUE, sendToBack = TRUE),
                  label ="Rhondda Cynon Taf") %>%
      addPolygons(data=area3,weight=2,col = 'blue',
                  highlightOptions = highlightOptions(color='blue', opacity = 1, weight = 2, fillOpacity = 0, bringToFront = TRUE, sendToBack = TRUE),
                  label ="Bridgend")



  }) # end of foundational.map()

  # render foundational leaflet map
  output$map <- leaflet::renderLeaflet({

    # call reactive map
    foundational.map()

  }) # end of render leaflet

  # store the current user-created version
  # of the Leaflet map for download in
  # a reactive expression
  user.created.map <- reactive({

    # call the foundational Leaflet map
    foundational.map() %>%
      setView( lng = input$map_center$lng
               ,  lat = input$map_center$lat
               , zoom = input$map_zoom

      )
  }) # end of creating user.created.map()



  # create the output file name
  # and specify how the download button will take
  # a screenshot - using the mapview::mapshot() function
  # and save as a PDF
  output$dl <- downloadHandler(
    filename = paste0( Sys.Date()
                       , "_customLeafletmap"
                       , ".png"
    )
    , content = function(file){
      #saveWidget(user.created.map(), "temp.html", selfcontained = FALSE)
      #webshot("temp.html", file = file, cliprect = "viewport")
      mapshot( x = user.created.map()
               , file = file
               , cliprect = "viewport" # the clipping rectangle matches the height & width from the viewing port
               , selfcontained = FALSE # when this was not specified, the function for produced a PDF of two pages: one of the leaflet map, the other a blank page.
      )
    } # end of content() function
  ) # end of downloadHandler() function

} # end of server

# run the Shiny app
shinyApp(ui = ui, server = server)

# end of script #

```
ZoeStrawbridge/Mapping- documentation built on May 31, 2019, 4:53 a.m.